<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/commons/taglib.jsp"%>
<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>
<%@ attribute name="locationValue" type="java.lang.String" required="true" description="输入域经纬度value"%>
<%@ attribute name="addressValue" type="java.lang.String" required="true" description="输入域地址value"%>

<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=c6b70d1b3e424ba485ce8141f3f06317&plugin=AMap.Geocoder"></script>
<div class="input-group">
	<input id="${id}Location"  type="hidden" value="${locationValue}"/>
	<input id="${id}Address" type="hidden" value="${addressValue}" />
</div>

<div id="${id }MapContainer" style="width:100%;height:100%;"></div>

<script type="text/javascript">

	var lnglat = $("#${id}Location").val();
	var address = $("#${id}Address").val();
	
	var map = new AMap.Map('${id}MapContainer',{
	    zoom: 13,
	});
	
	if(lnglat){
		
		var ll = location.split(',');
		map.setCenter(new AMap.LngLat(ll[0],ll[1]));
		if(!address)regeocoder([ll[0],ll[1]],regeocoder_CallBack);
	}else{
		if(address)geocoder(address,geocoder_CallBack);
	}
	
	
	map.on('click', function(e) {
       // alert('您在[ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ]的位置点击了地图！');
       map.clearMap();
		var marker = new AMap.Marker({
            map: map,
            position: [ e.lnglat.getLng(),  e.lnglat.getLat()]
        });
        regeocoder(location,function(data){
        	var regeocode = data.regeocode;
        	$("#${id}Location").val(geocode.location.getLng() + ", " + geocode.location.getLat() );
        	$("#${id}Address").val(regeocode.formattedAddress);
        	var infoWindow = new AMap.InfoWindow({
	            content: regeocode.formattedAddress,
	            offset: {x: 0, y: -30}
	        });
	        marker.on("mouseover", function(e) {
	            infoWindow.open(map, marker.getPosition());
	        });
        });
        
    });
	
	 function addMarker( d) {
	        var marker = new AMap.Marker({
	            map: map,
	            position: [ d.location.getLng(),  d.location.getLat()]
	        });
	        $("#${id}Location").val(d.location.getLng() + ", " + d.location.getLat() );
        	$("#${id}Address").val(d.formattedAddress);
	        var infoWindow = new AMap.InfoWindow({
	            content: d.formattedAddress,
	            offset: {x: 0, y: -30}
	        });
	        marker.on("mouseover", function(e) {
	            infoWindow.open(map, marker.getPosition());
	        });
	    }
    //地址->坐标
    function geocoder(address,geocoder_CallBack) {
        var geocoder = new AMap.Geocoder({
            city: "010", //城市，默认：“全国”
            radius: 1000 //范围，默认：500
        });
        //地理编码,返回地理编码结果
        geocoder.getLocation(address, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                geocoder_CallBack(result);
            }
        });
    }
    function geocoder_CallBack(data) {
        //地理编码结果数组
        var geocodes = data.geocodes;
        if(geocodes.length>0){
        	
        	addMarker(geocodes[0]);
        }
        map.setFitView();
    }
    //坐标->地址
     function regeocoder(lnglatXY,regeocoder_CallBack) {  //逆地理编码
        var geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all"
        });        
        geocoder.getAddress(lnglatXY, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                regeocoder_CallBack(result);
            }
        });        
        
    }
    function regeocoder_CallBack(data) {
    	var regeocode = data.regeocode;
       
       	addMarker(regeocode);
        map.setFitView();
    }
	AMap.plugin(['AMap.ToolBar','AMap.Scale'],function(){
	    var toolBar = new AMap.ToolBar();
	    var scale = new AMap.Scale();
	    map.addControl(toolBar);
	    map.addControl(scale);
	});
</script>